/*
 * @(#)PaginationTemplate.java $version 2012-3-5
 * 
 * Copyright 2011 RogueDevelopment Gruop. All rights Reserved.
 * ROGUEDEVELOPMENT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nado.extensions.pagination;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.axis.utils.StringUtils;
import org.apache.commons.collections.MapUtils;

/**
 * @author LEESUNG
 * 
 */
public class PaginationTemplate {
	private String first;
	private String firstOff;
	private String prev;
	private String prevOff;
	private String index;
	private String indexFirst;
	private String indexLast;
	private String indexFirstCurrent;
	private String indexLastCurrent;
	private String indexCurrent;
	private String separator;
	private String next;
	private String nextOff;
	private String last;
	private String lastOff;

	public PaginationTemplate() {
	}

	public PaginationTemplate(String first, String firstOff, String prev,
			String prevOff, String index, String indexFirst, String indexLast,
			String indexFirstCurrent, String indexLastCurrent,
			String indexCurrent, String separator, String next, String nextOff,
			String last, String lastOff) {
		this.first = first;
		this.firstOff = firstOff;
		this.prev = prev;
		this.prevOff = prevOff;
		this.index = index;
		this.indexFirst = indexFirst;
		this.indexLast = indexLast;
		this.indexFirstCurrent = indexFirstCurrent;
		this.indexLastCurrent = indexLastCurrent;
		this.indexCurrent = indexCurrent;
		this.separator = separator;
		this.next = next;
		this.nextOff = nextOff;
		this.last = last;
		this.lastOff = lastOff;
	}

	public String getFirst() {
		return this.first;
	}

	public void setFirst(String first) {
		this.first = first;
	}

	public String getFirstOff() {
		return this.firstOff;
	}

	public void setFirstOff(String firstOff) {
		this.firstOff = firstOff;
	}

	public String getPrev() {
		return this.prev;
	}

	public void setPrev(String prev) {
		this.prev = prev;
	}

	public String getPrevOff() {
		return this.prevOff;
	}

	public void setPrevOff(String prevOff) {
		this.prevOff = prevOff;
	}

	public String getIndex() {
		return this.index;
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public String getIndexFirst() {
		return this.indexFirst;
	}

	public void setIndexFirst(String indexFirst) {
		this.indexFirst = indexFirst;
	}

	public String getIndexLast() {
		return this.indexLast;
	}

	public void setIndexLast(String indexLast) {
		this.indexLast = indexLast;
	}

	public String getIndexFirstCurrent() {
		return this.indexFirstCurrent;
	}

	public void setIndexFirstCurrent(String indexFirstCurrent) {
		this.indexFirstCurrent = indexFirstCurrent;
	}

	public String getIndexLastCurrent() {
		return this.indexLastCurrent;
	}

	public void setIndexLastCurrent(String indexLastCurrent) {
		this.indexLastCurrent = indexLastCurrent;
	}

	public String getIndexCurrent() {
		return this.indexCurrent;
	}

	public void setIndexCurrent(String indexCurrent) {
		this.indexCurrent = indexCurrent;
	}

	public String getSeparator() {
		return this.separator;
	}

	public void setSeparator(String separator) {
		this.separator = separator;
	}

	public String getNext() {
		return this.next;
	}

	public void setNext(String next) {
		this.next = next;
	}

	public String getNextOff() {
		return this.nextOff;
	}

	public void setNextOff(String nextOff) {
		this.nextOff = nextOff;
	}

	public String getLast() {
		return this.last;
	}

	public void setLast(String last) {
		this.last = last;
	}

	public String getLastOff() {
		return this.lastOff;
	}

	public void setLastOff(String lastOff) {
		this.lastOff = lastOff;
	}

	public static String process(String templateString, Map<String, String> params) {
		
		if (StringUtils.isEmpty(templateString)) {
			return "";
		}
		if (MapUtils.isEmpty(params)) {
			return templateString;
		}
		
		String expressionRegex = "#\\{\\s*(\\w+)\\s*\\}";
		Matcher matcher = Pattern.compile(expressionRegex).matcher(templateString);
		StringBuffer sb = new StringBuffer();
		
		while (matcher.find()) {
			String variableName = matcher.group(1);

			if (params.containsKey(variableName)) {
				matcher.appendReplacement(sb, Matcher.quoteReplacement((String) params.get(variableName)));
			}
		}

		matcher.appendTail(sb);

		return sb.toString();
	}
}
